<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
*/

require_once JPATH_BASE.DIRECTORY_SEPARATOR.join(DIRECTORY_SEPARATOR,array('templates','hiled','menu','autoloader.php'));

/**
 * Description of ContentInjector
 *
 * @author chaporgin_a
 */
class VA_Menu_Helper_ContentInjector {
    /**
     *
     * @var JMenuSite
     */
    protected $menu;
    /**
     *
     * @var stdClass
     */
    protected $activeMenu;
    protected $children;
    protected $items;
    protected $open;
    protected $menuParams;

    /**
     *
     * @var JA_HiledMenu
     */
    private $menuLoader;
    /**
     * actually repeats code from JA_Base class, if it's not in your joomla then download it to find out
     *
     * 
     */
    public function  __construct() {
        $user =& JFactory::getUser();
        $this->menu = JSite::getMenu();
        $this->activeMenu = $this->menu->getActive();
        $cacheIndex = array();
        $rows = $this->menu->getItems('menutype', 'mainmenu');
        $children = array ();
        foreach ($rows as $index => $v) {
            if ($v->access <= $user->get('gid')) {
                $pt = $v->parent;
                $list = @ $children[$pt] ? $children[$pt] : array ();
                $v->_idx = count($list);
                array_push($list, $v);
                $children[$pt] = $list;
            }
            $cacheIndex[$v->id] = $index;
            $this->items[$v->id] = $v;
        }

        $open = array (
                $this->activeMenu->id
        );
        $count = 40; // maximum levels - to prevent runaway loop
        $id = $this->activeMenu->id;

        while (-- $count) {
            if (isset($cacheIndex[$id])) {
                $index = $cacheIndex[$id];
                if (isset ($rows[$index]) && $rows[$index]->parent > 0) {
                    $id = $rows[$index]->parent;
                    $open[] = $id;
                } else {
                    break;
                }
            }
        }
        $this->open = $open;
        $this->children = $children;
        //var_dump($this->menu->getItem($this->activeMenu->id));
        $this->menuParams = new JParameter( $this->activeMenu->params );
    }

    public function __toString() {
        global $mainframe;
        $headtag = array();
        $headtag[] = '<link rel="stylesheet" href="/templates/hiled/css/abstract_menu.css" type="text/css" />';
        $headtag[] = '<script src="/templates/hiled/js/abstract_menu.js" type="text/javascript"></script>';
        $mainframe->addCustomHeadTag(implode("\n", $headtag));
        $return = '';
        $menu = '';
        if(!empty ($this->children[$this->activeMenu->id])){
            foreach ($this->children[$this->activeMenu->id] as $element) {
                $menu .= $this->printMenuElement($element);
            }
        }
        $attr = new VA_AttributeDictionary();
        $attr->Set('class', 'abstract-menu');
        $return .= $this->wrapMenuElements($menu, $attr);

        return $return;
    }

    /**
     *
     * @param string $menu
     * @param VA_AttributeDictionary $attributes
     * @return string
     */
    protected function wrapMenuElements($menu, VA_AttributeDictionary $attributes){
        return sprintf('<div %s><ul>%s</ul></div>', $attributes, $menu);
    }

    protected function printMenuElement($element){
        $return = '';
        $p = new JParameter( $element->params );
        $desc = $p->def('description', null);
        $return .= sprintf('<li><div class="element"><a href="%s"><span>%s</span><div>%s</div></a></div></li>'
                , JRoute::_($element->link.'&Itemid='.$element->id)
                , $element->name
                , $desc
                );
        return $return;
    }
}
?>
